package ruc.InfluxDB;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import ruc.Consts.constant;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class InfluxDB_Common {
    private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient().newBuilder()
            .readTimeout(50000, TimeUnit.MILLISECONDS)
            .connectTimeout(50000, TimeUnit.MILLISECONDS)
            .writeTimeout(50000, TimeUnit.MILLISECONDS)
            .build();

    public static OkHttpClient getClient() {
        return OK_HTTP_CLIENT;
    }

    public static String DataUtil(long timeStamp) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
    }

    public static long sendRequest(Request request) {
        try {
            long startTime = System.nanoTime();
            getClient().newCall(request).execute();
            return (System.nanoTime() - startTime) / constant.Ns2Ms;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
